/**
 * Copyright (c) 2012-present 铭软科技(mingsoft.net)
 * 本软件及相关文档文件（以下简称“软件”）的版权归 铭软科技 所有
 * 遵循 铭软科技《服务协议》中的《保密条款》
 */








package net.mingsoft.basic.action.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.action.BaseAction;
import net.mingsoft.basic.bean.CityBean;
import net.mingsoft.basic.biz.ICityBiz;
import net.mingsoft.basic.entity.CityEntity;
import net.mingsoft.basic.util.BasicUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 省市县镇村数据管理控制层
 * @author 铭飞开发团队
 * @version
 * 版本号：100<br/>
 * 创建日期：2017-7-27 14:47:29<br/>
 * 历史修订：<br/>
 */
@Api(tags={"前端-基础接口"})
@Controller("webCityAction")
@RequestMapping("/basic/city")
public class CityAction extends BaseAction{

	/**
	 * 注入省市县镇村数据业务层
	 */
	@Autowired
	private ICityBiz cityBiz;

	@ApiOperation(value = "根据id查询下一级城市数据列表集合")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "provinceId", value = "省／直辖市／自治区级id", required = false,paramType="query"),
			@ApiImplicitParam(name = "cityId", value = "市级id", required = false,paramType="query"),
			@ApiImplicitParam(name = "countyId", value = "县／区级id", required = false,paramType="query"),
			@ApiImplicitParam(name = "townId", value = "街道／镇级id", required = false,paramType="query"),
	})
	@GetMapping("/list")
	@ResponseBody
	public ResultData list(@ApiIgnore @ModelAttribute CityEntity city){
		// 构造city实体属性的查询
		LambdaQueryWrapper<CityEntity> wrapper = new LambdaQueryWrapper<>(city);
		// 根据city的传递的id判断是否要进行分组，获取到该id下一级的集合
		wrapper.groupBy(CityEntity::getProvinceId)
				.groupBy(city.getProvinceId()!=null,CityEntity::getCityId)
				.groupBy(city.getCityId()!=null,CityEntity::getCountyId)
				.groupBy(city.getCountyId()!=null,CityEntity::getTownId)
				.groupBy(city.getTownId()!=null,CityEntity::getVillageId);
		List<CityEntity> cityList = cityBiz.list(wrapper);
		return ResultData.build().success(cityList);
	}


	@ApiOperation(value = "获取省市县镇村数据")
	@ApiImplicitParam(name = "id", value = "城市主键编号", required = true,paramType="query")
	@GetMapping("/get")
	@ResponseBody
	public ResultData get(@ModelAttribute @ApiIgnore CityEntity city, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model){
		if(StringUtils.isEmpty(city.getId())) {
			return ResultData.build().error( getResString("err.error", this.getResString("id")));
		}
		CityEntity _city = (CityEntity)cityBiz.getEntity(Integer.parseInt(city.getId()));
		return ResultData.build().success( _city);
	}


	@ApiOperation(value = "获取省市县镇村数据树形列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "level", value = "省市县层级，默认为3", required = false, paramType = "query",dataType = "int"),
		@ApiImplicitParam(name = "type", value = "结构类型，默认为tree", required = false, paramType = "query")
	})
	@GetMapping("/query")
	@ResponseBody
	@Deprecated
	public ResultData query(HttpServletResponse response,HttpServletRequest request) {
		int level = BasicUtil.getInt("level",3);//默认3级
		String type = BasicUtil.getString("type","tree"); //默认为树形结构
		List<CityBean> cityList = (List<CityBean>) cityBiz.queryForTree(level,type);
		return ResultData.build().success(cityList);
	}





}
